package com.base.common.model.http.jackSon;


import com.base.common.netBeanPackage.BaseResponse;
import com.base.common.utils.JavaMethod;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.type.SimpleType;

import java.io.IOException;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by yangkuo on 2018-09-29.
 */
public class JacksonResponseBodyConverterCustomer<T> implements Converter<ResponseBody, T> {
    private final ObjectReader adapter;


    JacksonResponseBodyConverterCustomer(ObjectReader adapter) {
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        // {"errno":1, "data":"Required Long parameter 'id' is not present", "errmsg":"处理异常" }
        String ss = value.source().readUtf8();
//        Object obj = JacksonUtils.getJsonBean(ss, Object.class);
        int code = 0;
        String msg = "";
        try {

//            if (obj instanceof Map) {
//                BaseResponse baseResponse = new BaseResponse();
//                Map map = (Map) obj;
//                code = JavaMethod.transformClass(map.get("code"), int.class, -1);
//                int errno = JavaMethod.transformClass(map.get("errno"), int.class, -1);
//                if (code != -1) {
//                    msg = JavaMethod.transformClass(map.get("msg"), String.class);
//                    baseResponse.setCode(code);
//                    baseResponse.setMsg(msg);
//
//                    Object data = map.get("data");
//                    Object list = map.get("list");
//                    Object pageBean = map.get("page");
//                    //对data和list为空的数，转换后再返回
//                    if (!UIUtils.isEmpty(data) || !UIUtils.isEmpty(list) || !UIUtils.isEmpty(pageBean)) {
//                        return adapter.readValue(ss);
//                    } else if (data == null && list == null && pageBean == null) {
//                        return adapter.readValue(ss);
//                    } else {
//                        return adapter.readValue(JacksonUtils.transBean2Json(baseResponse));
//                    }
//                } else if (errno != -1) {
//                    code = errno;
//                    msg = JavaMethod.transformClass(map.get("errmsg"), String.class);
//                    String data = JavaMethod.transformClass(map.get("data"), String.class);
//                    baseResponse.setCode(code);
//                    baseResponse.setMsg(msg + "  " + data);
//                    return adapter.readValue(JacksonUtils.transBean2Json(baseResponse));
//                }
//
//            }

            return adapter.readValue(ss);

        } catch (JsonMappingException e) {
            LogUtil.e(e.getMessage());
            LogUtil.e("解析错误，正在使用自定义解析.....");
            SimpleType simpleType = JavaMethod.getFieldValue(adapter, "_valueType", SimpleType.class);
            try {
                Object obj = JacksonUtils.getJsonBean(ss, Object.class);
                T bean = JacksonUtils.transformMap2Bean(simpleType, (Map) obj);
                LogUtil.e("自定义解析成功!");
                return bean;
            } catch (Exception e1) {
                LogUtil.e("自定义解析失败");
                Class clsBase = simpleType.getRawClass();
                try {
                    Object bean = clsBase.newInstance();

                    code = 301;
                    msg = "解析错误";
                    if (bean instanceof Map) {
                        Map map = (Map) bean;
                        map.put("code", code);
                        map.put("message", msg);
                    } else if (JavaMethod.isObject(clsBase)) {
                        JavaMethod.setFieldValue(bean, "code", code);
                        JavaMethod.setFieldValue(bean, "message", msg);
                    }


                    return (T) bean;
                } catch (IllegalAccessException | InstantiationException ex) {
                    ex.printStackTrace();
                }

                return null;
            }

        } finally {
            value.close();
        }
    }

}
